import maya.cmds as mc

class LZ_universal_spline():
    
    
    def create_universal_spline_commands(self , currentCtrl = '' , allJnt = [ '', '',''] ,rotateXAttr = 'rotateX'  , closetRatio = 0 , fatherRatio = 1  ):
        
        rotation_bridge = str(currentCtrl)+'_rotation_bridge'
        if mc.objExists(rotation_bridge) == 0:
            mc.group(em=True, n = rotation_bridge )
            
            for at in ['tx','ty','tz','rx','ry','rz','sx','sy','sz','v']:
                mc.setAttr( str(rotation_bridge)+'.' + str(at) , lock=True, k=False  )
            
        
        offsetAttr = ['range','offset' ,'current_closest','current_father','closet_ratio','father_ratio' ]  
        
        for oa in offsetAttr:
            if mc.attributeQuery( oa , node = currentCtrl , ex = True ) == 0:
                
                if oa  == 'range':
                    attrTyepe = 'long'
                else:
                    
                    attrTyepe = 'float'
                    
                if mc.attributeQuery( oa , node = currentCtrl , ex = True ) == 0:
                    mc.addAttr( currentCtrl , longName = oa ,attributeType = attrTyepe )
                    mc.setAttr( str(currentCtrl) + '.' + str(oa) , k = True )
                
                if oa  == 'range':
                    mc.addAttr( str(currentCtrl) + '.' + str(oa) ,e=True,dv =4 ,min = 1 ,at = 'long')
                    mc.setAttr( str(currentCtrl) + '.' + str(oa) , 3 )
                
                elif oa  == 'current_closest':
                    mc.setAttr( str(currentCtrl) + '.' + str(oa) , closetRatio ) 
                
                elif oa  == 'current_father':
                    mc.setAttr( str(currentCtrl) + '.' + str(oa) , fatherRatio ) 
                
                elif oa  == 'closet_ratio':
                    mc.setAttr( str(currentCtrl) + '.' + str(oa) , 1 ) 
                
                else:
                    pass
        
        #@####
        #@    
        #@closest_num_choice = str(currentCtrl) + '_closest_num_choice'
        #@
        #@if mc.objExists( closest_num_choice ) == 0 :
        #@    
        #@    mc.createNode( 'choice' , n = closest_num_choice  )
        #@
        #@father_num_choice = str(currentCtrl) + '_father_num_choice'
        #@
        #@if mc.objExists( father_num_choice ) == 0 :
        #@    
        #@    mc.createNode( 'choice' , n = father_num_choice  )
        #@    
        #@
        ####
        basePrefix = 'ratio'
        numSufix  = 'num'
        num = 0
        choiceNode_all = []
        
        for aj in allJnt:
        
            attrAll = [ 'global_scale','jntNum' ]
            
            ####
            
            closest_num_choice = str(aj) + '_' +  str(currentCtrl) + '_closest_num_choice'
            
            if mc.objExists( closest_num_choice ) == 0 :
                
                mc.createNode( 'choice' , n = closest_num_choice  )
            
            #father_num_choice = str(aj) + '_father_num_choice'
            #
            #if mc.objExists( father_num_choice ) == 0 :
            #    
            #    mc.createNode( 'choice' , n = father_num_choice  )
                
            ######
            for aal in attrAll:
            
                if mc.attributeQuery( aal , node = aj , ex = True ) == 0:
                
                    mc.addAttr( aj , longName = aal ,  attributeType = 'float' )
                    mc.setAttr( str(aj) + '.' + str(aal) , cb = True )
                    
                    if aal == 'jntNum':
                        mc.setAttr( str(aj) + '.' + str(aal) , k = True )
                        mc.setAttr( str(aj) + '.' + str(aal) , num )
                    
                
            
            attr = str(aj) + '_' + basePrefix
            
            if mc.attributeQuery( attr , node = rotation_bridge , ex = True ) == 0:
                mc.addAttr( rotation_bridge , longName = attr ,  attributeType = 'float' )
                mc.setAttr( str(rotation_bridge) + '.' + str(attr) , cb = True )
            
            #mc.connectAttr( str(rotation_bridge) + '.' + str(attr) , str(aj) + '.rotateX' )
            
            
            attr = str(aj) + '_' + numSufix
            
            if mc.attributeQuery( attr , node = currentCtrl , ex = True ) == 0:
                mc.addAttr( currentCtrl , longName = attr ,  attributeType = 'float' )
                mc.setAttr( str(currentCtrl) + '.' + str(attr) , cb = True )
                mc.setAttr( str(currentCtrl) + '.' + str(attr) , num)
            
            
            
            
            
            ####
            
            dif_num_PMA = str(aj)  + '_' +  str(currentCtrl) + '_diff_num_PMA'
            
            if mc.objExists( dif_num_PMA ) == 0 :
                mc.createNode( 'plusMinusAverage' , n = dif_num_PMA  )
                mc.connectAttr( str(currentCtrl) + '.current_closest' , str(dif_num_PMA) + '.input3D[0].input3Dx' )
                #mc.connectAttr( str(currentCtrl) + '.current_father' , str(dif_num_PMA) + '.input3D[1].input3Dy' )
                
                mc.connectAttr( str(aj) + '.jntNum' , str(dif_num_PMA) + '.input3D[1].input3Dx' )
                #mc.connectAttr( str(aj) + '.jntNum' , str(dif_num_PMA) + '.input3D[1].input3Dx' )
                
                mc.setAttr(  str(dif_num_PMA) + '.operation' , 2 )
            
            
            ####
            
            posneg_MD = str(aj)  + '_' +  str(currentCtrl) + '_posneg_MD'
            if mc.objExists( posneg_MD ) == 0 :
                mc.createNode( 'multiplyDivide' , n = posneg_MD  )
                
                mc.connectAttr( str(dif_num_PMA) + '.output3Dx' , str(posneg_MD) + '.input1X' )
                mc.setAttr(  str(posneg_MD) + '.input2X' , -1  )
                
            
            
            ####
            
            posneg_CON = str(aj)  + '_' +  str(currentCtrl) + '_posneg_CON'
            
            if mc.objExists( posneg_CON ) == 0 :
                mc.createNode( 'condition' , n = posneg_CON  )
                
                mc.connectAttr( str(dif_num_PMA) + '.output3Dx' ,  str( posneg_CON )+'.firstTerm' )
                mc.connectAttr( str(dif_num_PMA) + '.output3Dx'  ,  str( posneg_CON )+'.colorIfFalseR' )
                mc.connectAttr( str(posneg_MD) + '.outputX' ,  str( posneg_CON )+'.colorIfTrueR' )
                mc.setAttr( str( posneg_CON )+'.operation' ,  4 )
            
            
            ####
            action_num_PMA = str(aj)  + '_' +  str(currentCtrl) + '_action_num_PMA'
            
            if mc.objExists( action_num_PMA ) == 0 :
            
                mc.createNode( 'plusMinusAverage' , n = action_num_PMA  )
                
                mc.connectAttr( str(currentCtrl) + '.range' , str(action_num_PMA) + '.input3D[0].input3Dx' )
                #mc.connectAttr( str(currentCtrl) + '.range' , str(action_num_PMA) + '.input3D[0].input3Dy' )
                
                mc.connectAttr( str(posneg_CON) + '.outColorR' , str(action_num_PMA) + '.input3D[1].input3Dx' )
                #mc.connectAttr( str(dif_num_PMA) + '.output3Dy' , str(action_num_PMA) + '.input3D[1].input3Dy' )
                
                mc.setAttr(  str(action_num_PMA) + '.operation' , 2 )
            
            if mc.listConnections( str(closest_num_choice) + '.selector' , s=True ,d=False ) == None :
                mc.connectAttr( str(action_num_PMA) + '.output3Dx'  , str(closest_num_choice) + '.selector' ) 
            
            #@choiceNode_all.append( closest_num_choice )
            
            diff_inversevalue_PMA = str(aj)  + '_' +  str(currentCtrl)  + '_diff_inversevalue_PMA'
            diff_value_PMA = str(aj) + '_' +  str(currentCtrl)  + '_diff_value_PMA'
            
            if mc.objExists(diff_value_PMA) == 0 :
                mc.createNode( 'plusMinusAverage'  , n = diff_value_PMA )
            
            if mc.objExists(diff_inversevalue_PMA) == 0 :
                mc.createNode( 'plusMinusAverage'  , n = diff_inversevalue_PMA )
                
            mc.connectAttr( str( currentCtrl ) + '.closet_ratio' , str( diff_inversevalue_PMA )+'.input3D[1].input3Dx'  )
            mc.setAttr( str( diff_inversevalue_PMA )+'.input3D[0].input3Dx',1 )
            mc.setAttr( str( diff_inversevalue_PMA )+'.operation',2 )
            
            #@mc.connectAttr( str( closest_num_choice ) + '.output' , str( diff_value_PMA )+'.input3D[0].input3Dx'  )
            mc.connectAttr( str(action_num_PMA) + '.output3Dx' , str( diff_value_PMA )+'.input3D[0].input3Dx'  )
            
            
            mc.connectAttr( str( diff_inversevalue_PMA ) + '.output3Dx' , str( diff_value_PMA )+'.input3D[1].input3Dx'  )
            mc.setAttr( str( diff_value_PMA )+'.operation',2 )
            
            value_ratio_CON = str(aj)  + '_' +  str(currentCtrl) + '_value_ratio_CON'
            
            if mc.objExists(value_ratio_CON) == 0 :
                mc.createNode( 'condition'  , n = value_ratio_CON )
            
            mc.connectAttr( str( diff_value_PMA ) + '.output3Dx' , str( value_ratio_CON )+'.firstTerm'  )
            mc.connectAttr( str( diff_value_PMA ) + '.output3Dx' , str( value_ratio_CON )+'.colorIfTrueR'  )
            mc.setAttr( str( value_ratio_CON )+'.operation',3 )
            mc.setAttr( str( value_ratio_CON )+'.colorIfFalseR',0 )
            
            
            
            
            
            

            
            
            ########### ------------- jnt rotation value -----------------
            
            ####
            
            ####
            attrBridge = str(aj) + '_' + basePrefix  
            
            rotationV_MD = str(aj)  + '_' +  str(currentCtrl) + '_rotationV_MD'
            if mc.objExists( rotationV_MD ) == 0 :
                mc.createNode( 'multiplyDivide' , n = rotationV_MD  )
                
                mc.connectAttr( str( value_ratio_CON ) + '.outColorR' , str(rotationV_MD) + '.input1X' )
                
            ####
            
            rangeMD = str(aj)  + '_' +  str(currentCtrl) + '_range_MD'
            if mc.objExists( rangeMD ) == 0 :
                mc.createNode( 'multiplyDivide' , n = rangeMD  )
                
                mc.connectAttr( str(currentCtrl) + '.offset'   , str(rangeMD) + '.input1X' )
                mc.connectAttr( str(currentCtrl) + '.range'  , str(rangeMD) + '.input2X' )
                mc.setAttr( str(rangeMD) + '.operation' ,  2 )
                
                mc.connectAttr( str(rangeMD) + '.outputX'  , str(rotationV_MD) + '.input2X' )

                #mc.connectAttr(  str(rotationV_MD) + '.outputX' , str(aj)+'.' + str(rotateXAttr) )

            
            ########
            
            
            value_PMA = str(aj)   + '_value_PMA'
            
            if mc.objExists(value_PMA) == 0 :
                mc.createNode( 'plusMinusAverage'  , n = value_PMA )
                
            connections = mc.listConnections( value_PMA , s=True ,d=False )
            if connections == None :
                num_value = 0
            else:
                num_value = len(connections)
            
            #mc.connectAttr(  str(rotationV_MD) + '.outputX' , str(aj)+'.' + str(rotateXAttr) )
            
            if mc.listConnections( str( value_PMA ) + '.input3D['+ str(num_value) + '].input3Dx' , s = True ,d = False  ) == None :
                mc.connectAttr( str(rotationV_MD) + '.outputX'  , str( value_PMA ) + '.input3D['+ str(num_value) + '].input3Dx'  )
            
                      
            if mc.listConnections( str(aj)+'.' + str(rotateXAttr) , s = True ,d = False  ) == None :
                mc.connectAttr( str( value_PMA ) + '.output3Dx' , str(aj)+'.' + str(rotateXAttr)  )
            
            
            num = num +1
        #for cn in choiceNode_all:
        #    num = 0    
        #    for attr in  mc.listAttr( currentCtrl , cb =True  ) :
        #        
        #        mc.connectAttr( str(currentCtrl) + '.' + str(attr)  , str(cn) + '.input['  +str(num)+']' ) 
        #    
        #        num = num +1 
        #    
        
        
        
        
        
            
        
        
        
        
        
                #mc.connectAttr( str(action_num_PMA) + '.output3Dy'  , str(father_num_choice) + '.selector' ) 
                #mc.connectAttr( str(currentCtrl) + '.' + str(attr)  , str(father_num_choice) + '.input['  +str(num)+']' ) 
                #
                
            ####
            #
            #action_num_choice = str(aj) + '_action_num_choice'
            #
            #if mc.objExists( action_num_choice ) == 0 :
            #    
            #    mc.createNode( 'choice' , n = action_num_choice  )
            #    
            #    mc.connectAttr( str(currentCtrl) + '.range' , str(action_num_PMA) + '.input3D[0].input3Dx' )
            #    mc.connectAttr( str(currentCtrl) + '.range' , str(action_num_PMA) + '.input3D[0].input3Dy' )
            #    
            #    mc.connectAttr( str(dif_num_PMA) + '.output3Dx' , str(action_num_PMA) + '.input3D[1].input3Dy' )
            #    mc.connectAttr( str(dif_num_PMA) + '.output3Dy' , str(action_num_PMA) + '.input3D[1].input3Dx' )
            #    
            
            
            
 